package com.wang.interceptor;

import com.wang.enums.BizCodeEnum;
import com.wang.util.CommonUtil;
import com.wang.util.JWTUtil;
import com.wang.model.LoginUser;
import com.wang.util.JsonData;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    public static  ThreadLocal<LoginUser> threadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            //前端传来的token可能在请求头，也可能在Get请求类型的请求参数中
            String accessToken = request.getHeader("token");
            if (accessToken == null) {
                accessToken = request.getParameter("token");
            }

            if (StringUtils.isNotBlank(accessToken)) {
                Claims claims = JWTUtil.checkJWT(accessToken);
                if (claims == null) {
                    //告诉登录过期，重新登录
                    CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
                    return false;
                }

                Long id = Long.valueOf(claims.get("id").toString());
                String headImg = (String) claims.get("head_img");
                String mail = (String) claims.get("mail");
                String name = (String) claims.get("name");
                Integer sex =  Integer.valueOf (claims.get("sex").toString().toString());
                Integer points =  Integer.valueOf(claims.get("points").toString());
                String slogan = (String) claims.get("slogan");

                LoginUser loginUser = LoginUser.builder()
                        .id(id)
                        .headImg(headImg)
                        .name(name)
                        .mail(mail)
                        .points(points)
                        .sex(Integer.valueOf(sex))
                        .slogan(slogan)
                        .build();

//                loginUser.setId(id);
//                loginUser.setName(name);
//                loginUser.setHeaderImg(headImg);
//                loginUser.setMail(mail);

                //用户信息传递,使用ThreadLocal   后面要获取时，直接从threadLocal.get()获取就行
                threadLocal.set(loginUser);

                return true;

            }

        } catch (Exception e) {
            log.error("拦截器错误:{}",e);
        }


        CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
        return false;

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
}
